package com.sinosoft.brokerchannel.server;

import com.sinosoft.brokerchannel.base.common.SystemParamFinal;
import com.sinosoft.brokerchannel.base.utile.BusinessException;
import com.sinosoft.brokerchannel.base.utile.DateUtils;
import com.sinosoft.brokerchannel.entity.vo.CarActualValueCalculationRequest;
import com.sinosoft.brokerchannel.entity.vo.CarActualValueCalculationRequestMain;
import com.sinosoft.brokerchannel.entity.vo.CarActualValueCalculationResponse;
import com.sinosoft.brokerchannel.entity.vo.CarActualValueCalculationResponseMain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @需求名称:实际价值计算
 * @类描述:实际价值计算实现类
 */
@Service
public class CarActualValueCalculationService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * @param request({@link CarActualValueCalculationRequest})
     * @return ({@link CarActualValueCalculationResponse})
     * @throws BusinessException
     * @需求名称：实际价值计算
     * @描述:实际价值计算实现方法
     */
    public CarActualValueCalculationResponse carActualValueCalculation(CarActualValueCalculationRequest request) throws BusinessException {
        CarActualValueCalculationResponse response;
        try {
            response = this.actualValue(request);
        } catch (BusinessException e) {
            logger.info(SystemParamFinal.Exception_LOG, e);
            throw e;
        } catch (Exception e) {
            logger.info(SystemParamFinal.Exception_LOG, e);
            throw new BusinessException("CarActualValueCalculation-0003", "计算车辆实际价值异常");
        }
        return response;
    }

    /**
     * @param attr
     * @param common
     * @return
     * @throws BusinessException
     * @需求名称：实际价值计算
     * @描述:非空校验
     */
    @SuppressWarnings("unused")
    private <T> void verifyEmpty(T attr, String common) throws BusinessException {
        if (null == attr || "".equals(attr)) {
            throw new BusinessException("CarActualValueCalculation-0001", common + "不能为空");
        }
    }

    /**
     * @param resquest ：车辆实际价值请求对象 {@link CarActualValueCalculationRequest}
     * @return 车辆实际价值返回对象
     * @throws Exception
     * @描述:车辆实际价值计算方法
     */
    private CarActualValueCalculationResponse actualValue(CarActualValueCalculationRequest resquest) throws BusinessException {
        //返回信息
        CarActualValueCalculationResponse response = new CarActualValueCalculationResponse();
        CarActualValueCalculationResponseMain responseBody = new CarActualValueCalculationResponseMain();
        response.setResponseBody(responseBody);
        Double actualValue = 0.0d;//车辆实际价值
        Double despMons = 1d;//折旧月数
        Double monDespRate = 0.0d;//月折旧率
        try {
            CarActualValueCalculationRequestMain requestBody = resquest.getRequestBody();
            String insrncBgnTm = requestBody.getInsrncBgnTm();//保险起期
            String cFstRegYm = requestBody.getcFstRegYm();//初等日期
            String usageCde = requestBody.getcUsageCde();//使用性质
            Double newPurchaseValue = Double.parseDouble(requestBody.getnNewPurchaseValue());//新车购置价
            Integer seatNum = Integer.parseInt(requestBody.getnSeatNum());//座位数
            String vhlTyp = requestBody.getcVhlTyp();//车辆种类
            String vhlSubTyp = requestBody.getcVhlSubTyp(); //车辆种类细分
            //车辆实际价值计算
            //非营运客车 ,家庭自用客车   ,机关非营业汽车  ,非营运客车
            if (usageCde != null && (usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364111001) || usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113003) || usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113002))) {
                if (seatNum < 10) monDespRate = 0.006;
                else monDespRate = 0.009;
            }
            //营运客车其他,城市公交营业客车,公路客运营业客车
            if (usageCde != null && (usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113005) || usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113006))) {
                monDespRate = 0.009;
            }
            //营运客车出租车,出租、租赁营业客车
            if (usageCde != null && (usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113012))) {
                monDespRate = 0.011;
            }
            //出租、租赁营业客车(交强险)
            if (usageCde != null && (usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113004))) {
                monDespRate = 0.011;
            }
            //营业挂车(商改)
            if (usageCde != null && (usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113015))) {
                monDespRate = 0.009;
            }
            //非营业挂车(商改)
            if (usageCde != null && (usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113016))) {
                monDespRate = 0.009;
            }
            //非营业货车，营业货车，非营业货车	，营业货车
            if (usageCde != null && vhlTyp != null && (usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113007) || usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113009))) {
                //非营业货车，低速载货汽车
                if (usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113007) && !vhlTyp.equals(SystemParamFinal.VHL_CVHLTYP_365018)) {
                    monDespRate = 0.009;
                }
                //营业货车、低速载货汽车
                if (usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113009) && !vhlTyp.equals(SystemParamFinal.VHL_CVHLTYP_365018)) {
                    monDespRate = 0.011;
                }
                //非营业货车	、低速载货汽车
                if (usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113007) && vhlTyp.equals(SystemParamFinal.VHL_CVHLTYP_365018)) {
                    monDespRate = 0.011;
                }
                //营业货车											低速载货汽车
                if (usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113009) && vhlTyp.equals(SystemParamFinal.VHL_CVHLTYP_365018)) {
                    monDespRate = 0.014;
                }
                //营业货车	2吨以下或者10吨以上
                if (usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113009) && (vhlTyp.equals(SystemParamFinal.VHL_CVHLTYP_365009) || vhlTyp.equals(SystemParamFinal.VHL_CVHLTYP_365006))) {
                    monDespRate = 0.009;
                }
                if (usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113009) && (vhlTyp.equals(SystemParamFinal.VHL_CVHLTYP_365014) || vhlTyp.equals(SystemParamFinal.VHL_CVHLTYP_365015) || vhlTyp.equals(SystemParamFinal.VHL_CVHLTYP_365016) || vhlTyp.equals(SystemParamFinal.VHL_CVHLTYP_365017))) {
                    monDespRate = 0.009;
                }
            }
            //特种车处理
            if (usageCde != null && vhlTyp != null && usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364115030) && vhlTyp.equals(SystemParamFinal.VHL_CUSAGECDE_364115030) && vhlSubTyp.equals(SystemParamFinal.VHL_CSUBTYP_379007)) {
                monDespRate = 0.011;
            }
            if (usageCde != null && vhlTyp != null && usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364115030) && vhlTyp.equals(SystemParamFinal.VHL_CUSAGECDE_364115030) && vhlSubTyp.equals(SystemParamFinal.VHL_CSUBTYP_379007)) {
                monDespRate = 0.009;
            }
            if (usageCde != null && vhlTyp != null && usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364115030) && !vhlTyp.equals(SystemParamFinal.VHL_CUSAGECDE_364115030)) {
                monDespRate = 0.009;
            }
            // 营业特种车
            if (usageCde != null && usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113017)) {
                monDespRate = 0.009;
            }
            // 非营业特种车
            if (usageCde != null && usageCde.equals(SystemParamFinal.VHL_CUSAGECDE_364113018)) {
                monDespRate = 0.009;
            }
            //计算月份数
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = formatter.parse(cFstRegYm);//初登日期，字符串型转换为日期型
            } catch (Exception e) {
                logger.info(SystemParamFinal.Exception_LOG, e);
                throw new BusinessException("CarActualValueCalculation-0004", "日期格式异常");
            }
            try {
                despMons = (double) DateUtils.compareMonth(date, formatter.parse(insrncBgnTm));
                //计算总折旧率
                BigDecimal despRateBig;
                BigDecimal monDespRateBig = BigDecimal.valueOf(monDespRate);
                despRateBig = BigDecimal.valueOf(despMons).multiply(monDespRateBig);// 总折旧率 使用月乘以月折旧率
                // 折旧率大于80%时，按80%计算
                if (despRateBig.compareTo(BigDecimal.valueOf(0.8)) > 0) {
                    despRateBig = BigDecimal.valueOf(0.8);
                }
                BigDecimal newPurchaseValueBig = BigDecimal.valueOf(newPurchaseValue);//新车购置价
                BigDecimal actualValueBig;//实际价值
                BigDecimal despValueBig = newPurchaseValueBig.multiply(despRateBig);//折旧价值
                BigDecimal maxDespRate = BigDecimal.valueOf(0.8);//最大折旧系数
                if (despValueBig.compareTo(newPurchaseValueBig.multiply(maxDespRate)) > 0) {
                    despValueBig = newPurchaseValueBig.multiply(maxDespRate);
                }
                //计算实际价值
                actualValueBig = newPurchaseValueBig.subtract(despValueBig);
                actualValue = actualValueBig.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();// 车的实际价值
            } catch (Exception e) {
                logger.info(SystemParamFinal.Exception_LOG, e);
                throw new BusinessException("CarActualValueCalculation-0004", "计算实际价值异常");
            }
            responseBody.setActualValue(actualValue + "");//实际价值
            responseBody.setDespRate(monDespRate + "");//折旧率
        } catch (BusinessException e) {
            logger.info(SystemParamFinal.Exception_LOG, e);
            throw e;
        } catch (Exception e) {
            logger.info(SystemParamFinal.Exception_LOG, e);
            throw new BusinessException("CarActualValueCalculation-0004", "计算实际价值异常");
        }
        return response;
    }
}

